#region Using

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;
using System.Web;
using BlogEngine.Core;

#endregion

namespace BlogEngine.Core.StorageProviders
{
	/// <summary>
	/// The proxy class for communication between an storage object and an underlying provider
	/// the business objects and the providers.
	/// </summary>
	public static class StorageProviderService
	{

		#region Provider model

		private static StorageProvider _PhotoProvider;
		private static StorageProvider _VideoProvider;
		private static StorageProviderCollection _providers;
		private static object _lock = new object();

		/// <summary>
		/// Gets the current provider.
		/// </summary>
		public static StorageProvider PhotoProvider
		{
			get
			{
				LoadProviders();
				return _PhotoProvider;
			}
		}
		/// <summary>
		/// Gets the current provider.
		/// </summary>
		public static StorageProvider VideoProvider
		{
			get
			{
				LoadProviders();
				return _VideoProvider;
			}
		}

		/// <summary>
		/// Gets a collection of all registered providers.
		/// </summary>
		public static StorageProviderCollection Providers
		{
			get
			{
				LoadProviders();
				return _providers;
			}
		}

		/// <summary>
		/// Load the providers from the web.config.
		/// </summary>
		private static void LoadProviders()
		{
			lock (_lock)
			{
				// Do this again to make sure _provider is still null
				if (_providers == null)
				{
					// Get a reference to the <photoProvider> section
					StorageProviderSection photoSection = (StorageProviderSection)WebConfigurationManager.GetSection("BlogEngine/photoProvider");

					_providers = new StorageProviderCollection();

					// Load registered providers and point _provider to the default provider
					StorageProviderCollection myPhotoProviders = new StorageProviderCollection();
					ProvidersHelper.InstantiateProviders(photoSection.Providers, myPhotoProviders, typeof(StorageProvider));
					_PhotoProvider = myPhotoProviders[photoSection.DefaultProvider];

					foreach (StorageProvider pp in myPhotoProviders)
						_providers.Add(pp);

					// Get a reference to the <videoProvider> section
					StorageProviderSection videoSection = (StorageProviderSection)WebConfigurationManager.GetSection("BlogEngine/videoProvider");

					// Load registered providers and point _provider to the default provider
					StorageProviderCollection myVideoProviders = new StorageProviderCollection();
					ProvidersHelper.InstantiateProviders(videoSection.Providers, myVideoProviders, typeof(StorageProvider));
					_VideoProvider = myVideoProviders[videoSection.DefaultProvider];

					foreach (StorageProvider vp in myVideoProviders)
						_providers.Add(vp);

				}
			}
		}


		#endregion

		#region Storage Management


		#endregion


	}
}
